using System;
using System.Collections.Generic;
using System.Text;

using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;

using UncleGED.Application;
using UncleGED.Collections;
using UncleGED.Documentation;
using UncleGED.Utils;

namespace UGed
{
   public static class ConfigReader
   {

      #region Config Settings
      /// <summary>
      /// Summary of SaveXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// <param name=ConfigSettings></param>
      /// 		
      public static void SaveProject(string projectFile, UGedProject projectData)
      {
         XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
         FileStream fStream = new FileStream(projectFile, FileMode.Create);
         XmlS.Serialize(fStream, projectData);
         fStream.Close();
      }

      /// <summary>
      /// Summary of LoadXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// 		
      public static UGedProject LoadProject(string projectFile)
      {
         UGedProject configSettings;

         try
         {
            XmlSerializer XmlS = new XmlSerializer(typeof(UGedProject));
            FileStream fStream = new FileStream(projectFile, FileMode.Open);
            configSettings = (UGedProject)XmlS.Deserialize(fStream);
            fStream.Close();
            fStream = null;
            XmlS = null;
         }
         catch 
         {           
            configSettings = new UGedProject();
            SaveProject(projectFile, configSettings);
         }

         return configSettings;

      }
      #endregion

      #region  Local application
      public static void SaveSettings(string SettingsFile, LocalApplication SettingsData)
      {
         XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
         FileStream fStream = new FileStream(SettingsFile, FileMode.Create);
         XmlS.Serialize(fStream, SettingsData);
         fStream.Close();
      }

      /// <summary>
      /// Summary of LoadXmlSettings.
      /// </summary>
      /// <param name=sXmlConfig></param>
      /// 		
      public static LocalApplication LoadSettings(string SettingsFile)
      {
         LocalApplication configSettings;

         try
         {
            XmlSerializer XmlS = new XmlSerializer(typeof(LocalApplication));
            FileStream fStream = new FileStream(SettingsFile, FileMode.Open);
            configSettings = (LocalApplication)XmlS.Deserialize(fStream);
            fStream.Close();
            fStream = null;
            XmlS = null;
         }
         catch
         {
            configSettings = new LocalApplication();
            SaveSettings(SettingsFile, configSettings);
         }

         return configSettings;

      }

      #endregion

   }
}
